package com.vip.admin.config;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.vip.admin.common.annotation.OperationLog;
import com.vip.admin.common.constant.CommonConstant;
import com.vip.admin.common.utils.IPUtils;
import com.vip.admin.common.utils.SpringContextUtils;
import com.vip.admin.modules.system.entity.SysLog;
import com.vip.admin.modules.system.entity.SysUser;
import com.vip.admin.modules.system.service.ISysLogService;

/**
 * 操作日志，切面处理类
 * 
 * @author Administrator
 *
 */
@Aspect
@Component
public class OperationLogAspect {
	@Autowired
	private ISysLogService sysLogService;

	@Pointcut("@annotation(com.vip.admin.common.annotation.OperationLog)")
	public void logPointCut() {

	}

	@Around("logPointCut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		long beginTime = System.currentTimeMillis();
		// 执行方法
		Object result = point.proceed();
		// 执行时长(毫秒)
		long time = System.currentTimeMillis() - beginTime;
		// 保存日志
		saveSysLog(point, time);
		return result;
	}

	private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();

		SysLog sysLog = new SysLog();
		OperationLog syslog = method.getAnnotation(OperationLog.class);
		if (syslog != null) {
			// 注解上的描述,操作日志内容
			sysLog.setLogContent(syslog.logContent());
			sysLog.setLogType(syslog.logType());
		}

		// 请求的方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		sysLog.setMethod(className + "." + methodName + "()");

		// 设置操作类型
		if (sysLog.getLogType() == CommonConstant.LOG_TYPE_2) {
			sysLog.setOperateType(getOperateType(methodName, syslog.operateType()));
		}

		// 请求的参数
		Object[] args = joinPoint.getArgs();
		try {
			String params = JSONObject.toJSONString(args);
			sysLog.setRequestParam(params);
		} catch (Exception e) {

		}

		// 获取request
		HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
		// 设置IP地址
		sysLog.setIp(IPUtils.getIpAddrByRequest(request));

		// 获取登录用户信息
		SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
		if (sysUser != null) {
			sysLog.setUsername(sysUser.getUserName());
			sysLog.setRealname(sysUser.getRealName());
		}
		// 耗时
		sysLog.setCostTime(new BigDecimal(time).multiply(new BigDecimal(.001)));
		sysLog.setCreateTime(new Date());
		// 保存系统日志
		sysLogService.save(sysLog);
	}

	/**
	 * 获取操作类型
	 */
	private int getOperateType(String methodName, int operateType) {
		if (operateType > 0) {
			return operateType;
		}
		if (methodName.startsWith("list")) {
			return CommonConstant.OPERATE_TYPE_SELECT;
		}
		if (methodName.startsWith("add")) {
			return CommonConstant.OPERATE_TYPE_INSERT;
		}
		if (methodName.startsWith("edit")) {
			return CommonConstant.OPERATE_TYPE_UPDATE;
		}
		if (methodName.startsWith("delete")) {
			return CommonConstant.OPERATE_TYPE_DELETE;
		}
		if (methodName.startsWith("import")) {
			return CommonConstant.OPERATE_TYPE_IMPORT;
		}
		if (methodName.startsWith("export")) {
			return CommonConstant.OPERATE_TYPE_EXPORT;
		}
		return CommonConstant.OPERATE_TYPE_SELECT;
	}
}
